
/**
 * Created by wxnacy on 2015/8/12.
 */
$(function() {
    //数据准备工作
    initData()
    //加载table数据
    initPage();

});
//数据准备工作
function initData(){
    //列表中的时间显示
    $('.time-td').each(function(){
        var date = $(this).text()
        if(date ==''){
            return ;
        }
        console.log(date);
        var dataStr = $.timeStampFormat(parseInt(date),"yyyy/MM/dd HH:mm:ss")
        $(this).text(dataStr)
    })
    //显示列表中的图片
    $('.pic-td').each(function(){
        //var $img = $(this).find('img');
        var $obj = $(this);
        var json = $obj.text();
        var data = $.evalJSON(json);
        $.each(data.data,function(i,item){
            if(i ==0){
                var imgUrl = item.url;
                $obj.text('')
                var $img = $('<img src="'+imgUrl+'" />');
                $obj.append($img)
            }
        })
    })
}


//加载页面js
function initPage(){
    var dataUrl = $('#listTable').attr('data-url');
    var $searchForm = $('#seachForm');
    //console.log(dataUrl)
    //翻页数据
    $('#ul-page').find('a').each(function(e){
        $(this).click(function(e){
            var page = $(this).attr('page');
            console.log($searchForm.serialize());
            $(this).attr('href',dataUrl+'?page='+page+'&'+$searchForm.serialize());
        });
    });
    //跳转页码
    $('a:contains("跳转")').click(function(){
        var dataUrl = $('#listTable').attr('data-url');
        var page = $('input[name="page"]').val();
        $(this).attr('href',dataUrl+'?page='+page+'&'+$searchForm.serialize());
    });
    //跳转页码
    $('button:contains("返回")').click(function(){
        history.go(-1);
    });
    //button按钮的跳转
    $('button[data-url]').click(function(){
        if($(this).hasClass('save-next')){
            return;
        }
        var dataUrl = $(this).attr('data-url');
        var target = $(this).data("target");
        if(target == "new"){
            var id = $(this).attr('id');
            var text = $(this).data('text');
            console.log(text);
            if(text=='undefined'){
                text = $(this).find('span').text();
            }
            top.clickNev(id,dataUrl,text)
        }else{
            window.location.href = dataUrl;
        }

    });
    //button按钮的异步跳转
    $('button[ajax-url]').click(function(){
        var _this = $(this);
        var ajaxUrl = _this.attr('ajax-url');
        //批量操作的情况
        var musicIds='';
        if(_this.hasClass('batch')){
            var $checkboxs = $('tbody :checkbox');
            $checkboxs.each(function(){
                var $this = $(this);
                if(this.checked){
                    musicIds += $this.val()+',';
                }
            });
            if(musicIds == ''){
                swal('操作失败', '请至少选择一条数据', 'error');
                return;
            }
            musicIds = musicIds.substring(0,musicIds.lastIndexOf(','));
            console.log(musicIds,musicIds.length);
            ajaxUrl = ajaxUrl+musicIds;
        }


        //操作前的提醒
        swal({
            title: '确认此操作吗?',
            //text: 'You will not be able to recover this imaginary file!',
            type: 'warning',
            showCancelButton: true,
            //confirmButtonColor: '#DD6B55',
            confirmButtonText: '确定!',
            closeOnConfirm: false,
                cancelButtonText:'取消'
        },
        function () {
            //确定后的操作
            $.ajax({
                type: "get",
                dataType: "json",
                url: ajaxUrl,
                error: function () {
                    alert('error');
                },
                success: function (data) {
                    if(data.success){
                        swal({
                            title: data.msg,
                            text: '点击返回',
                            type:'success'
                        },function(){
                            //判断是否操作以后需要关闭窗口
                            if($('.fa-remove',_this).length>0){
                                _this.parent().parent().remove();
                            }else{
                                location.reload();
                            }
                        });
                    }else{
                        swal('操作失败', data.msg, 'error');
                    }
                }
            });
        });



    });

    //全选按钮
    $('.select-all').click(function(e){
        e.preventDefault();
        var $checkboxs = $('tbody :checkbox');
        $checkboxs.prop("checked", true);
    });
    //反选
    $('.select-reverse').click(function(e){
        e.preventDefault();
        var $checkboxs = $('tbody :checkbox');
        $checkboxs.each(function(){
            var $this = $(this);
            if(this.checked){
                $this.prop("checked", false);
            }else{
                $this.prop("checked", true);

            }

        });
    });
}
